package zhaoxue.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import zhaoxue.entity.BjEntity;
import zhaoxue.entity.Page;
import zhaoxue.entity.ZhaoXueEntity;

import java.util.List;
@Component()
public class ZhaoXueDao extends HibernateDaoSupport {
    @Transactional
    public void save(ZhaoXueEntity zhaoXueEntity) {
        Session session= getSessionFactory().openSession();
        session.saveOrUpdate(zhaoXueEntity);
        session.flush();
    }

    public int queryCount(String name) {
        String sql = "select * from j05zhaoxue where name like ? ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setParameter(0,  "%" + name + "%");
        List<ZhaoXueEntity> list=  query.list();
        return  list.size();
    }

    public List<ZhaoXueEntity> queryPage(Page page, String name) {
        String sql = "select a.name,a.id,a.url,b.bname from j05zhaoxue a,j05zxclazz b where a.bid=b.id and  name like ? order by systime desc limit ?,? ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setParameter(0,  "%" + name + "%");
        query.setParameter(1,  page.getBeginIndex());
        query.setParameter(2,  page.getEveryPage());
        query.setResultTransformer(Transformers.aliasToBean(ZhaoXueEntity.class));
        List<ZhaoXueEntity> list=  query.list();
        return  list;
    }

    public ZhaoXueEntity edit(ZhaoXueEntity zhaoXueEntity) {
        Session session= getSessionFactory().openSession();
        return (ZhaoXueEntity)session.get(ZhaoXueEntity.class,zhaoXueEntity.getId());
    }

    public void del(ZhaoXueEntity zhaoXueEntity) {
        Session session= getSessionFactory().openSession();
        session.delete(zhaoXueEntity);
        session.flush();
    }

    public List<BjEntity> bj() {
        String sql = "select * from j05zxclazz  ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setResultTransformer(Transformers.aliasToBean(BjEntity.class));
        List<BjEntity> list=  query.list();
        return  list;
    }
}
